import { getAllRoleList, getDeptList, getDistrictCascader } from '/@/api/demo/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { checkID } from '/@/utils/index';
import debounce from 'lodash.debounce';
import { TreeSelect } from 'ant-design-vue';
const { SHOW_PARENT, SHOW_ALL, SHOW_CHILD } = TreeSelect;
import { useUserStore } from '/@/store/modules/user';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { encryptByMd5, strongPwdRegex } from '/@/utils/cipher';

const userStore = useUserStore();
export const columns: BasicColumn[] = [

  {
    title: '姓名',
    dataIndex: 'fzxm',
  },
  {
    title: '区',
    dataIndex: 'xzq',
  },
  {
    title: '街乡镇',
    dataIndex: 'jxz',
  },
  {
    title: '村',
    dataIndex: 'cz',
  },
  {
    title: '角色',
    dataIndex: 'role_type',
  },
  {
    title: '是否解绑',
    dataIndex: 'disabled',
  },
   {
    title: '企安安id',
    dataIndex: 'qaakey',
  }
];

export const searchDistrictFormSchema: FormSchema[] = [
  // {
  //   field: 'districts_',
  //   label: '行政区划',
  //   component: 'ApiCascader',
  //   labelWidth: '10em',
  //   componentProps: ({ formModel, formActionType }) => {
  //     return {
  //       api: getDistrictCascader,
  //       apiParamKey: 'parentId',
  //       labelField: 'regionName',
  //       valueField: '_d',
  //       dataField: 'regionCode',
  //       asyncFetchParamKey: 'parentId',
  //       multiple: true,
  //       initFetchParams: {
  //         parentId: '',
  //       },
  //       isLeaf: (record) => {
  //         return record.regionalLevel > 6;
  //       },
  //     };
  //   },
  // },
];

// export const searchFormSchema: FormSchema[] = searchDistrictFormSchema.concat([


// export const searchDistrictFormSchema: FormSchema[] = [
//     {
//       field: 'districts_',
//       label: '行政区划',
//       component: 'ApiCascader',
//       labelWidth: '6em',
//       colProps: { span: 6 },
//       componentProps: ({ formModel, formActionType }) => {
//         return {
//           api: getDistrictCascader,
//           apiParamKey: 'parentId',
//           labelField: 'regionName',
//           valueField: '_d',
//           dataField: 'regionCode',
//           asyncFetchParamKey: 'parentId',
//           multiple: true,
//           initFetchParams: {
//             parentId: '',
//           },
//           isLeaf: (record) => {
//             return record.regionalLevel > 6;
//           },
//         };
//       },
//     },

  
//   ];

export const searchFormSchema: FormSchema[] = searchDistrictFormSchema.concat([
  {
    field: 'fzxm',
    label: '绑定姓名',
    component: 'Input',
    labelWidth: '6em',
    colProps: { span: 6 },
  },
  {
    field: 'roletype',
    label: '账号角色',
    component: 'Select',
    componentProps: {
      options: [
        { label: '村级角色', value: 5 },
        { label: '乡镇角色', value: 4 },
        { label: '区级角色', value: 3 },
        { label: '市级角色', value: 2 },
      ],
      placeholder: '请选择',
    },
    labelWidth: '6em',
    colProps: { span: 6 },
  },
]);

  
 // ]);

  
// export const searchFormSchema: FormSchema[] = [
 
//   {
//     field: 'districtCode_',
//     label: '行政区',
//     component: 'Select',
//     labelWidth: '6em',
//     colProps: { span: 6 },
   
//   },
//   {
//     field: 'fzxm',
//     label: '绑定姓名',
//     component: 'Input',
//     labelWidth: '6em',
//     colProps: { span: 6 },
//   },

//   //  {
//   //   field: 'jurisdiction',
//   //   label: '村名',
//   //   component: 'Input',
//   //   labelWidth: '4em',
//   //   colProps: { span: 4 },
//   // },
  
// ];
// admin 编辑密码
export const accountPwdFormSchema: FormSchema[] = [
  {
    field: 'pwd',
    label: '密码',
    defaultValue: '',
    component: 'InputPassword',
    rules: [
      {
        required: true,
        message: '请输入新密码',
      },
      {
        pattern: strongPwdRegex,
        // message: '密码必须为八位及以上并且大小写字母数字特殊字符三项都要包括',
        message: '密码中必须包含字母、数字、特殊字符，(长度在 8-30位)！',
      },
    ],
  },
];

function visiFormItem(values) {
  const ignores = ['UnitAdmin', 'UnitInvestigator', 'CityUnitAdmin', 'CityUnitInvestigator'];
  return !ignores.includes(values['role']);
}

function autoFillForms(formModel, keys) {
  const ignores = ['UnitAdmin', 'UnitInvestigator', 'CityUnitAdmin', 'CityUnitInvestigator'];

  if (ignores.includes(keys)) {
    formModel.userGroup = userStore.getUserInfo.userGroup;
    if (userStore.getUserInfo.districtJson)
      formModel.districtCode_ = JSON.parse(userStore.getUserInfo.districtJson);

    if (userStore.getUserInfo.jurisdictionJson) {
      formModel.jurisdictionCodes_ = JSON.parse(userStore.getUserInfo.jurisdictionJson);
    }
  }
}

export const accountFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户名',
    defaultValue: '',
    component: 'Input',
    helpMessage: ['登录账号'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
    ],
  },
  {
    field: 'nickname',
    label: '姓名',
    component: 'Input',
    required: true,
  },
  // 新增里的 密码
  {
    field: 'pwd',
    label: '密码',
    component: 'InputPassword',
    required: true,
    ifShow: true,
    rules: [
      {
        required: true,
        message: '请输入新密码',
      },
      {
        pattern: strongPwdRegex,
        // message: '密码必须为八位及以上并且大小写字母数字特殊字符三项都要包括',
        message: '密码中必须包含字母、数字、特殊字符，(长度在 8-30位)！',
      },
    ],
  },
  {
    field: 'phone',
    label: '手机',
    component: 'Input',
    required: true,
    rules: [
      {
        required: true,
        message: '请输入手机号',
      },
      {
        pattern: new RegExp(/^1[345678]\d{9}$/),
        message: '请输入正确的手机号',
      },
    ],
  },
  {
    field: 'idCard',
    label: '身份证号',
    component: 'Input',
    required: true,
    rules: [
      {
        required: true,
        message: '请输入身份证号',
      },
      // {
      //   validator(_, value, _callback) {
      //     return new Promise((resolve, reject) => {
      //       if (checkID(value)) {
      //         resolve();
      //       } else {
      //         reject('请输入正确的身份证号');
      //       }
      //     });
      //   },
      //   message: '请输入正确的身份证号',
      // },
    ],
  },
  {
    label: '角色',
    field: 'role',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getAllRoleList,
        labelField: 'name',
        valueField: 'role',
        onChange: (keys, args, apiData, options) => {
          console.log(keys);
          console.log(args);
          console.log(formModel['role']);
          autoFillForms(formModel, keys);
        },
      };
    },
    required: true,
  },
  {
    field: 'userGroup',
    label: '用户组',
    component: 'ApiSelect',
    componentProps: {
      api: getDeptList,
      labelField: 'name',
      valueField: 'group',
    },
    required: true,
    show: ({ values }) => visiFormItem(values),
  },
  {
    field: 'districtCode_',
    component: 'ApiCascader',
    helpMessage: ['双击也可以选择'],
    label: '行政区',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getDistrictCascader,
        apiParamKey: 'parentId',
        labelField: 'regionName',
        valueField: '_d',
        dataField: 'regionCode',
        asyncFetchParamKey: 'parentId',
        initFetchParams: {
          parentId: '',
        },
        isLeaf: (record) => {
          return record.regionalLevel > 6;
        },
        onChange: (keys, args, apiData, options) => {
          formModel.jurisdictionCodes_ = [formModel.districtCode_];
        },
      };
    },
    required: true,
    show: ({ values }) => visiFormItem(values),
  },
  {
    field: 'jurisdictionCodes_',
    component: 'ApiCascader',
    helpMessage: ['双击也可以选择'],
    label: '管辖区',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getDistrictCascader,
        apiParamKey: 'parentId',
        labelField: 'regionName',
        valueField: '_d',
        dataField: 'regionCode',
        asyncFetchParamKey: 'parentId',
        multiple: true,
        initFetchParams: {
          parentId: '',
        },
        isLeaf: (record) => {
          return record.regionalLevel > 6;
        },
        onChange: (keys, args, apiData, options) => {},
      };
    },
    required: true,
    show: ({ values }) => visiFormItem(values),
  },
];
